//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using Content = CommunityServer.Components.Content;

using CommunityServer.Components;

namespace CommunityServer.Components 
{
	/// <summary>
	/// Content class for CMS functionality
	/// </summary>
	public class Content : IContent
	{
		private const string _unknown = "UnknownContent";

		private int _id = 0;
		private string _name = null;
		private string _title = null;
		private string _body = null;
		private string _formattedbody = null;
		private DateTime _modified = DateTime.MinValue;
		private bool _exists = false;
		private bool _hidden = false;

		public Content()
		{
		}

		/// <summary>
		/// create a content class and populate 
		/// with specific data
		/// </summary>
		public Content(int id, string name, string title, string body, string formattedbody, DateTime modified, bool hidden)
		{
			_id = id;
			_name = name;
			_title = title;
			_body = body;
			_modified = modified;
			_exists = !Globals.IsNullorEmpty(body);
			_hidden = hidden;
			// HACK: not implemented FormattedBody correctly yet
			_formattedbody = formattedbody;
		}

		public Content Copy()
		{
			return new Content(this.ID, this.Name, this.Title, this.Body, this.FormattedBody, this.LastModified, this.Hidden);
		}




		#region Properties

		/// <summary>
		/// return the HTML content 
		/// </summary>
		public string Body
		{
			get {return _body;}
			set {_body = value;}
		}

		/// <summary>
		/// returns true if content is real
		/// </summary>
		public bool Exists
		{
			get {return _exists;}
		}

		/// <summary>
		/// return the HTML content 
		/// </summary>
		public string FormattedBody
		{
			get {return _formattedbody;}
			set {_formattedbody = value;}
		}
		
		/// <summary>
		/// return the HTML content, processed for tokens
		/// Note that this is not cached today, in case there are user-, 
		/// page- or time-specific tokens in the body that will give 
		/// different results depending on context. 
		/// </summary>
		public virtual string RenderedBody(PostTarget target)
		{
			Content c = this.Copy();

			// pass FormattedBody through CSModule to process tokens
			if (c.Exists)
				CSEvents.PreRenderPost(c, ApplicationType.ContentManagement,target);

			return c.FormattedBody;
		}

		/// <summary>
		/// true if content is hidden from listings and searches
		/// </summary>
		public bool Hidden
		{
			get {return _hidden;}
			set {_hidden = value;}
		}
		
		/// <summary>
		/// the unique integer ID of the content
		/// NOTE: setting this value will overwrite other member variables
		/// </summary>
		public int ID
		{
			get { return _id; }
			set
			{
				_id = value;

				if (_id == 0)
				{
					_exists = false;
				}
			}
		}

		/// <summary>
		/// the last time the content was modified
		/// </summary>
		public DateTime LastModified
		{
			get {return _modified;}
		}

		/// <summary>
		/// the name of the content
		/// NOTE: setting this value will overwrite other member variables
		/// </summary>
		public string Name
		{
			get { return _name; }
			set {_name = value; }
		}

		/// <summary>
		/// The title (headline) of the content used in lists 
		/// and head title tag
		/// </summary>
		public string Title
		{
			get {return _title;}
			set {_title = value;}
		}
		#endregion
	}
}
